﻿#include "IOBase.h"
#include <QFile>
#include <QTextStream>
#include <QDomDocument>
#include <QDataStream>

namespace BaseModule
{
    IOBase::IOBase(const QString& fileName) : m_fileName(fileName)
    {
        m_file.setFileName(m_fileName);
    }

    IOBase::IOBase()
    {
    }

    IOBase::~IOBase()
    {
        if (m_stream) delete m_stream;
        if (m_doc) delete m_doc;
        if (m_dataStream) delete m_dataStream;
    }

    void IOBase::setFileName(const QString& name)
    {
        m_fileName = name;
        m_file.setFileName(m_fileName);
    }

    bool IOBase::read()
    {
        return false;
    }

    bool IOBase::write(QString s)
    {
        return false;
    }

    void IOBase::readRealTimeData(QList<QVector<double>>& xs, QList<QVector<double>>& ys, QString fileName)
    {
        this->setFileName(fileName);
        if (!m_file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            return;
        }

        m_stream = new QTextStream(&m_file);
        QVector<double> _x; 
        QVector<double> _y;
        QString _line;
        QStringList _xy;
        while (!m_stream->atEnd())
        {
            _line = m_stream->readLine().simplified();
            if (_line.isEmpty()) continue;
            _xy = _line.split(" ");
            if (_xy.size() > 1)
            {
                _x.append(_xy[0].toDouble());
                _y.append(_xy[1].toDouble());
            }
        }
        xs.append(_x);
        ys.append(_y);
    }
}
